package com.woniu.springbootshiro.controller;

import com.woniu.springbootshiro.pojo.GroupLight;
import com.woniu.springbootshiro.service.GroupLightService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
public class GroupLightController {

    @Autowired
    private GroupLightService gls;

    @RequestMapping("/queryAll")
    @ResponseBody
    public List<GroupLight> quertAll() {
        return gls.queryAll();
    }

    @RequestMapping("/addGroup")
    @ResponseBody
    public String addGroup(GroupLight groupLight){

        String type="0";
        if(gls.addGroupLight(groupLight)>0){
            type="1";
        }
        System.out.println(type);
        return type;
    }

    @RequestMapping("/deleteGroupLight")
    public String deleteGroup(int id){
        gls.deleteGroupLight(id);
        return null;
    }

    @RequestMapping("/cookieGroupLight")
    public String cookieGroupLight(int pid, HttpServletRequest req, HttpServletResponse resp){
        String pidString=pid+"";
        System.out.println(pidString);
        Cookie cookie=new Cookie("pidString",pidString);
        resp.addCookie(cookie);
        return null;
    }

    @RequestMapping("/updateGroupLight")
    public String updateGroupLight(@CookieValue(value = "pidString", defaultValue = "Atta") String pidString,GroupLight groupLight) {
        int pid=Integer.parseInt(pidString);
        groupLight.setPid(pid);
        gls.updateGroupLight(groupLight);
        return null;
    }

}
